package com.jack.leetcode.每日一题;

public class 最长回文子串 {

    public static void main(String[] args) {
        String s = "babad";
        System.out.println(longestPalindrome(s));
    }

    public static String longestPalindrome(String s) {
        int n = s.length();
        if (n == 0) {
            return "";
        }
        String result = s.substring(0, 1);
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 2; j <= n; j++) {
                String tmp = s.substring(i, j);
                if (tmp.length() > result.length() && isPal(tmp)) {
                    result = tmp;
                }
            }
        }
        return result;
    }

    private static boolean isPal(String s) {
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
